package com.test.fundible.fundible_02_permission;

import android.annotation.SuppressLint;
import android.content.Context;

import com.test.fundible.R;
import com.test.fundible.fundible_01_launcher.Base_fundibleApplication;

import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;


public class PermissionAdapter extends RecyclerView.Adapter<PermissionAdapter.PermissionHolder> {

    private final Context context;

    private final ArrayList<PermissionInfo> infos;

    public PermissionAdapter(Context context, ArrayList<PermissionInfo> infos){
        this.context = context;
        this.infos = infos;
    }

    public PermissionInfo getInfo(int index){
        return infos.get(index);
    }

    public boolean isPermissionHasHandle(int index){
        if (index<0 || index>=infos.size()) return false;
        PermissionInfo info = infos.get(index);
        if (info == null || info.getHolder() == null) return false;
        return info.getHolder().getStatus() != PermissionStatus.Default_fundible;
    }

    @NonNull
    @Override
    public PermissionHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new PermissionHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.view_permission,parent,false));
    }

    @SuppressLint("InflateParams")
    @Override
    public void onBindViewHolder(@NonNull PermissionHolder holder, int position) {
        infos.get(position).setHolder(holder);
        if (position==infos.size()-1) holder.setNextIvVisbility(View.GONE);
    }

    @Override
    public int getItemCount() {
        return infos.size();
    }

    public static class PermissionHolder extends RecyclerView.ViewHolder{

        private final View nextView;
        private final View nextIv;
        private final ImageView permissionIconIv;
        private final TextView permissionTitleTv;
        private final TextView permissionDescTv;
        private final View permissionCheckView;
        private final ImageButton permissionCheckIb;
        private final View denyBtn,agreeBtn;
        private final View btnView,authorizedView,bannedVeiw;

        private boolean isCheck = false;
        private PermissionStatus status = PermissionStatus.Default_fundible;

        @SuppressLint("InflateParams")
        public PermissionHolder(View view){

            super(view);

            nextView = view.findViewById(R.id.vp_view_next);
            nextIv = view.findViewById(R.id.vp_iv_next);
            permissionIconIv = view.findViewById(R.id.vp_iv_permission_icon);
            permissionTitleTv = view.findViewById(R.id.vp_tv_permission_title);
            permissionDescTv = view.findViewById(R.id.vp_tv_permission_desc);
            permissionCheckView = view.findViewById(R.id.vp_view_permission_check);
            permissionCheckIb = view.findViewById(R.id.vp_ib_permission_check);
            denyBtn = view.findViewById(R.id.vp_btn_deny);
            agreeBtn = view.findViewById(R.id.vp_btn_agree);
            btnView = view.findViewById(R.id.vp_view_btn);
            authorizedView = view.findViewById(R.id.vp_view_authorized);
            bannedVeiw = view.findViewById(R.id.vp_view_banned);

            permissionCheckIb.setOnClickListener(v -> setCheck(!isCheck));
        }

        public void setIcon(int drawableId){
            permissionIconIv.setBackgroundResource(drawableId);
        }

        public void setTitle(CharSequence title){
            permissionTitleTv.setText(title);
        }

        public void setNextClickListener(View.OnClickListener onClickListener){
            nextView.setOnClickListener(onClickListener);
        }

        public void setNextIvVisbility(int visbility){
            nextIv.setVisibility(visbility);
        }

        public void setDesc(int descId){
            permissionDescTv.setText(Base_fundibleApplication.getInstant().getString(descId));
        }

        public void setCheck(boolean isCheck){
            this.isCheck = isCheck;
            permissionCheckIb.setBackgroundResource(isCheck ? R.drawable.icon_check_true:R.drawable.icon_check_false);
        }

        public boolean isCheck() {
            return isCheck;
        }

        public void setPermissionStatus(PermissionStatus status) {
            this.status = status;
            switch (status){
                case Deny_fundible:
                    btnView.setVisibility(View.GONE);
                    permissionCheckView.setVisibility(View.GONE);
                    authorizedView.setVisibility(View.GONE);
                    bannedVeiw.setVisibility(View.VISIBLE);
                    break;
                case Agree_fundible:
                    btnView.setVisibility(View.GONE);
                    permissionCheckView.setVisibility(View.GONE);
                    authorizedView.setVisibility(View.VISIBLE);
                    bannedVeiw.setVisibility(View.GONE);
                    break;
                case Default_fundible:
                    btnView.setVisibility(View.VISIBLE);
                    permissionCheckView.setVisibility(View.VISIBLE);
                    authorizedView.setVisibility(View.GONE);
                    bannedVeiw.setVisibility(View.GONE);
                    break;
            }
        }

        public PermissionStatus getStatus() {
            return status;
        }

        public void setDenyClickListener(View.OnClickListener onClickListener){
            denyBtn.setOnClickListener(onClickListener);
        }

        public void setAgreeClickListener(View.OnClickListener onClickListener){
            agreeBtn.setOnClickListener(onClickListener);
        }


    }
}
